package com.nidu.demo.log.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.log.convertor.ApiAccessLogConvertor;
import com.nidu.demo.log.convertor.ApiAccessLogCriteriaConvertor;
import com.nidu.demo.log.dto.ApiAccessLogCO;
import com.nidu.demo.log.dto.ApiAccessLogPageQry;
import com.nidu.demo.log.gateway.ApiAccessLogGateway;
import com.nidu.demo.log.model.ApiAccessLog;
import com.nidu.demo.log.model.ApiAccessLogCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * API访问日志分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class ApiAccessLogPageQryExe {

    private final ApiAccessLogGateway gateway;
    private final ApiAccessLogConvertor convertor;
    private final ApiAccessLogCriteriaConvertor criteriaConvertor;

    public PageResponse<ApiAccessLogCO> execute(ApiAccessLogPageQry qry) {
        // 1. 转换查询条件
        ApiAccessLogCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<ApiAccessLog> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<ApiAccessLogCO> apiAccessLogCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(apiAccessLogCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
